package com.longcai.service.impl.scm;

import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homegood.ClyjGoodsSpec;
import com.longcai.entity.homegood.ClyjGoodsVo;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.entity.scm.ClyjScmGoods;
import com.longcai.entity.scm.ClyjScmGoodsSpec;
import com.longcai.entity.scm.ClyjScmGoodsVo;
import com.longcai.mapper.scm.ClyjScmGoodsMapper;
import com.longcai.mapper.goods.ClyjGoodsHomepageMapper;
import com.longcai.service.scm.ClyjScmGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 首页商品表 服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-04-23
 */
@Service
public class ClyjScmGoodsServiceImpl extends ServiceImpl<ClyjScmGoodsMapper, ClyjScmGoods> implements ClyjScmGoodsService {
	private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsHomepageMapper clyjGoodsHomepageMapper;
    
    @Autowired
    private ClyjScmGoodsMapper clyjMapper;
	@Override
	public List<GoodsClass> listByBrandId(String goodsId) {
		return clyjMapper.listByBrandId(goodsId);
	}
	//删除类型
	@Override
	public void deleteType(ClyjScmGoodsVo goodsClas2s) {
		clyjMapper.deleteType(goodsClas2s.getGoodsId());
	}
	//新增类型
	@Override
	public void insertType(ClyjScmGoodsVo goodsClas2s) {
		List<String> typeIds = goodsClas2s.getTypeIds();
		if(typeIds==null || typeIds.size()==0)
			return;
		for (String s : typeIds) {
			Map map = new HashMap();
			map.put("id", UUID.randomUUID().toString());
			map.put("goodsId", goodsClas2s.getGoodsId());
			map.put("typeId", s);
			clyjMapper.insertType(map);
		}
	}
	//删除规格
	@Override
	public void deleteSpec(ClyjScmGoodsVo goodsClas2s) {
		clyjMapper.deleteSpec(goodsClas2s.getGoodsId());
	}
	//新增规格
	@Override
	public void insertSpec(ClyjScmGoodsVo goodsClas2s) {
		List<String> specDetail = goodsClas2s.getSpecDetail();
		List<String> specSort = goodsClas2s.getSpecSort();
		List<String> specNum = goodsClas2s.getSpecNum();
		int num=0;
		if(specDetail==null || specDetail.size()==0)
			return;
		for (int i=0 ; i<specDetail.size();i++){
			Map map = new HashMap();
			map.put("id", UUID.randomUUID().toString());
			map.put("goodsId", goodsClas2s.getGoodsId());
			map.put("detail", specDetail.get(i));
			map.put("sort",specSort.get(i));
			map.put("num", specNum.get(i));
			clyjMapper.insertSpec(map);
			num = num+Integer.valueOf(specNum.get(i));
		}
		//修改商品库存
		ClyjScmGoods g = new ClyjScmGoods();
		g.setGoodsId(goodsClas2s.getGoodsId());
		g.setGoodsStock(num);
		clyjMapper.updateById(g);
	}
	@Override
	public List<ClyjScmGoodsSpec> getSpecByGoodsId(String goodsId) {
		// TODO Auto-generated method stub
		return clyjMapper.getSpecByGoodsId(goodsId);
	}
	@Override
	public void deletePics(ClyjScmGoodsVo goodsClas2s) {
		clyjMapper.deletePics(goodsClas2s.getGoodsId());
	}




}
